<?php
/* 站内信息类
 * @author  zhanghui
 * @create_date 2008-02-28
 */
class Message{

	/* 增加一条站内信息
	 * @param $conn 数据库连接类 
	 * @param : id,baby_id,baby_fid,friend_name,message_type(1:收件),title,message,read_type,create_date
	 */
	function addMessage($conn,$arr_input){
		$str_query = "insert into";
		$str_query .= " baby_message";
		$str_query .= " set";
		$str_query .= " baby_id={$arr_input['baby_id']}";
		$str_query .= ",baby_fid={$arr_input['baby_fid']}";
		$str_query .= ",friend_name='{$arr_input['friend_name']}'";
		$str_query .= ",message_type={$arr_input['message_type']}";
		$str_query .= ",title='{$arr_input['title']}'";
		$str_query .= ",message='{$arr_input['message']}'";
		$str_query .= ",create_date='".$conn->getCurrentTime()."'";
		
		$conn->executeQuery($str_query);
	}
	
	
	
	/* 删除一条站内信息 
	 * @param : id,baby_id,baby_fid,friend_name,message_type(1:收件),title,message,read_type,create_date
	 */
	function deleteMessage($conn,$arr_input){
		$str_query = "delete from";
		$str_query .= " baby_message";
		$str_query .= " where";
		$str_query .= " baby_id={$arr_input['baby_id']}";
		$str_query .= " and message_type={$arr_input['message_type']}";
		
		if(isset($arr_input['id'])){
			$str_query .= " and id={$arr_input['id']}";
		}
		
		$conn->executeQuery($str_query);
	}
	
	/* 获取站内信息 
	 * @param $conn 数据库连接类
	 * @param : id,baby_id,baby_fid,friend_name,message_type(1:收件),title,message,read_type,create_date
     * @param string $limit 
     * @return : $arr_output
     *			id, 			  [0]<br>
     *			baby_id, 		  [1]<br>
     *			baby_fid, 		  [2]<br>
     *			friend_name,   	  [3]<br>
     *			message_type, 	  [4]<br>
     *			title, 		      [5]<br>
     *			message, 		  [6]<br>
     *			read_type, 		  [7]<br>
     *			create_date, 	  [8]<br>
	 */
	function getMessageList($conn,$arr_input,$limit = null){
		$str_query = "select";
		if(isset($arr_input['cnt'])){
			$str_query .= " count(1)";
		}else{
			$str_query .= " id";
			$str_query .= ",baby_id";
			$str_query .= ",baby_fid";
			$str_query .= ",friend_name";
			$str_query .= ",message_type";
			$str_query .= ",title";
			$str_query .= ",message";
			$str_query .= ",read_type";
			$str_query .= ",create_date";
		}
		$str_query .= " from";
		$str_query .= " baby_message";
		$str_query .= " where";		
		if($arr_input['message_type'] == 1){             /* 收件箱 */
			$str_query .= " baby_id={$arr_input['baby_id']}";
			$str_query .= " and message_type={$arr_input['message_type']}";
			if(isset($arr_input['id'])){
				$str_query .= " and id={$arr_input['id']}";
			}
		}
		if($arr_input['message_type'] == 2){            /* 发件箱 */
			$str_query .= " baby_id={$arr_input['baby_id']}";
			$str_query .= " and message_type={$arr_input['message_type']}";
			if(isset($arr_input['id'])){
				$str_query .= " and id={$arr_input['id']}";
			}
		}
		if(isset($arr_input['orderby'])){
			$str_query .= " order by {$arr_input['orderby']}";
		}
		if($limit != null){
			$str_query .= " {$limit}";
		}
		$arr_output = $conn->selectQuery($str_query);	
	    return $arr_output;	    
	}
	
	/* 设置一条收信息的状态为已读 
	 * @param : id,baby_id,baby_fid,friend_name,message_type(1:收件),title,message,read_type,create_date
	 */
	function setMessageType($conn,$arr_input){
		$str_query = "update";
		$str_query .= " baby_message";
		$str_query .= " set";
		$str_query .= " read_type=1";
		$str_query .= " where";
		$str_query .= " baby_id={$arr_input['baby_id']}";
		$str_query .= " and id={$arr_input['id']}";
		
		$conn->executeQuery($str_query);
	}
}
?>